package com.xy.mainp.main.widget;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xy.AnimUtils;
import com.xy.debug.LogUtils;
import com.xy.debug.SystemConfig;
import com.xy.mainp.R;
import com.xy.mainp.databinding.ViewTittleBarBinding;
import com.xy.mainp.huaban.widget.HistoryPopup;
import com.xy.mainp.utils.StatusBarUtil;

/**
 * @author ocy
 * @time 2016/10/31 22:29
 * @des ${TODU}
 */

public class TittleBar extends FrameLayout {



    private int cevHeight;
    private ViewTittleBarBinding barBinding;

    public TittleBar(Context context) {
        this(context, null);
    }

    public TittleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TittleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public static TittleBar initTittleBar(Activity activity) {
        TittleBar tittleBar = new TittleBar(activity);
        tittleBar.attachedTo(activity);
        return tittleBar;
    }

    public static TittleBar initTittleBar(FrameLayout frameLayout) {
        TittleBar titlebar = new TittleBar(frameLayout.getContext());
        titlebar.attachedTo(frameLayout);
        return titlebar;
    }

    FrameLayout container;

    public FrameLayout getContainer() {
        return container;
    }

    public void attachedTo(FrameLayout frameLayout) {
        container = frameLayout;
        TittleBar oldTittleBar = (TittleBar) container.findViewById(R.id.mytitlebar);
        if (oldTittleBar != null) {
            setContentView(oldTittleBar.cevView);
            container.removeView(oldTittleBar);
            oldTittleBar.cevView = null;
        }
        this.setId(R.id.mytitlebar);
        frameLayout.addView(this, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void attachedTo(Activity activity) {
        FrameLayout decorRoot = initDecorRoot(activity);
        attachedTo(decorRoot);
    }

    private FrameLayout initDecorRoot(Activity activity) {
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        FrameLayout decorRoot = (FrameLayout) decorView.findViewById(R.id.decor_root);
        if (decorRoot == null) {
            FrameLayout framelayout = new FrameLayout(activity);
            framelayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            decorRoot = framelayout;
            decorRoot.setId(R.id.decor_root);
            ViewGroup decorChild = (ViewGroup) decorView.getChildAt(0);
            decorView.removeView(decorChild);
            decorRoot.addView(decorChild);
            decorView.addView(decorRoot);
        }
        return decorRoot;
    }

    private View cevView;

    public void setContentView(View cevView) {
        setContentView(cevView, null);
    }

    public void setContentView(View cevView, ViewGroup.LayoutParams params) {
        if (container == null || cevView == null) {
            return;
        }
        if (container.getId() == R.id.decor_root) {
            if (cevView.getParent() == null) {
                if (params == null) {
                    ((Activity) container.getContext()).getWindow().setContentView(cevView);
                } else {
                    ((Activity) container.getContext()).getWindow().setContentView(cevView, params);
                }
            }
        } else {
            if (cevView.getParent() == null) {
                container.removeView(this.cevView);
                if (params == null) {
                    container.addView(cevView);
                } else {
                    container.addView(cevView, params);
                }
            }
        }
        this.cevView = cevView;
        reSetRootViewTop();
        bringToFront();
    }

    private void init(Context context) {
        barBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_tittle_bar, null, false);

        cevHeight = SystemConfig.dp2px(getContext(), 48);
        barBinding.getRoot().setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, cevHeight));
        setBackgroundColor(getResources().getColor(R.color.title_bar));
        initTextType(context);
        addView(barBinding.getRoot());
    }

    private void initTextType(Context context) {
        Typeface fontFace = Typeface.createFromAsset(context.getAssets(),
                "fonts/xy_name.ttf");
        barBinding.tvTittlebarTittle.setTypeface(fontFace);
    }

    public void setTittle(String tittle) {
        barBinding.tvTittlebarTittle.setText(tittle);
        barBinding.tvTittlebarTittle.setVisibility(VISIBLE);
        barBinding.flEtTittlebar.setVisibility(GONE);
    }

    public void setTittle(CharSequence tittle) {
        barBinding.tvTittlebarTittle.setText(tittle);
        barBinding.tvTittlebarTittle.setVisibility(VISIBLE);
        barBinding.flEtTittlebar.setVisibility(GONE);
    }

    public void setTittle(View view) {
        barBinding.flEtTittlebar.setVisibility(GONE);
       barBinding.flTittlebarTittle.removeAllViews();
        barBinding.flTittlebarTittle.addView(view);
    }

    public void setVisEditText(boolean visEditText){
        barBinding.flEtTittlebar.setVisibility(visEditText ? VISIBLE:GONE);
        barBinding.tvTittlebarTittle.setVisibility(visEditText ? GONE:VISIBLE);
    }

    public EditText getEtTittle(){
        return barBinding.etTittlebar;
    }
    public static final int ITEM_SCAN = 0;
    public static final int ITEM_SEARCH = 1;

    public void setItemClick(int item,OnClickListener listener) {
        switch (item){
            case ITEM_SCAN:
                barBinding.ivTitlebarScan.setOnClickListener(listener);
                break;
            case ITEM_SEARCH:
                barBinding.ivTitlebarSearch.setOnClickListener(listener);
                break;
            default:
                LogUtils.w("has not this item");
                break;
        }
    }

    public void canSearch(boolean canSearch) {
        if (canSearch) {
            barBinding.ivTitlebarSearch.setVisibility(VISIBLE);

        } else {
            barBinding.ivTitlebarSearch.setVisibility(GONE);
        }
    }

    public void canBack(boolean back) {
        if (back) {
            barBinding.ivTitlebarBack.setVisibility(VISIBLE);
            barBinding.ivTitlebarBack.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    ((Activity)getContext()).finish();
                }
            });
        } else {
            barBinding.ivTitlebarBack.setVisibility(GONE);
        }
    }

    public void canScan(boolean canScan) {
        if (canScan) {
            barBinding.ivTitlebarScan.setVisibility(VISIBLE);
        } else {
            barBinding.ivTitlebarScan.setVisibility(GONE);
        }
    }

    private boolean isHideTitleBar = false;

    public void isHideTittleBar(boolean isHideTitleBar,boolean refHeight){
        if (this.isHideTitleBar == isHideTitleBar){
            return;
        }
        this.isHideTitleBar = isHideTitleBar;
        if (isHideTitleBar){
            AnimUtils.sildeOutToTop(this, 250);
        }else {
            AnimUtils.sildeInToTop(this, 250);
        }
        if (refHeight){
            reSetRootViewTop();
        }
        bringToFront();
    }

    public void reSetRootViewTop() {
        if (cevView == null) {
            return;
        }
        int uiOffsetTitlebar = 0;
        if (isHideTitleBar) {
            uiOffsetTitlebar = 0;
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && cevView.getFitsSystemWindows()) {
                uiOffsetTitlebar = cevHeight;
            } else {
                uiOffsetTitlebar = cevHeight + getPaddingTop();
            }
        }
        ViewGroup.LayoutParams layoutParams = cevView.getLayoutParams();
        if (layoutParams != null && layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) cevView.getLayoutParams();
            if (marginLayoutParams == null) {
                marginLayoutParams = new MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            }
            marginLayoutParams.topMargin = uiOffsetTitlebar;
            cevView.setLayoutParams(marginLayoutParams);
        }
    }
}
